package com.atstudent.gmall.cart.controller;

import com.atstudent.gmall.cart.entity.CartItem;
import com.atstudent.gmall.cart.service.CartService;
import com.atstudent.gmall.common.result.Result;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车功能
 * @Author 薛有朋
 * @CreatDate 2025/4/18 20:55
 **/

@RestController
@RequestMapping(value = "/api/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 查询购物车中的所有商品
     * @Author 薛有朋
     * @Date 2025/4/18 20:58
     **/
    @GetMapping(value = "/cartList")
    public Result<List<CartItem>> findAllCartItem(){
        List<CartItem> cartItemList = cartService.findAllCartItem();
        return Result.ok(cartItemList);
    }

    /**
     * 商品数量修改
     * @Author 薛有朋
     * @Date 2025/4/19 19:46
     **/
    @PostMapping(value = "/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable(value = "skuId") Long skuId , @PathVariable(value = "skuNum") Integer skuNum){
        cartService.addToCart(skuId , skuNum);
        return Result.ok();
    }

    /**
     * 商品选中不选中
     * @Author 薛有朋
     * @Date 2025/4/20 22:39
     **/
    @GetMapping(value = "/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable(value = "skuId") Long skuId , @PathVariable(value = "isChecked") Integer isChecked){
        cartService.checkCart(skuId , isChecked);
        return Result.ok();
    }

    /**
     * 删除单个商品-根据skuId
     * @Author 薛有朋
     * @Date 2025/4/21 20:09
     **/
    @DeleteMapping(value = "/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable(value = "skuId") Long skuId){
        cartService.deleteCart(skuId);
        return Result.ok();
    }
}
